<template>
  <div class="login">
    <div class="content">
      <h3>欢迎使用微卡共享支付系统</h3>
      <el-form :model="userInfo" ref="userInfo" :rules="rules" label-width="60px">
        <el-form-item label="账号:" prop="account" class="item">
          <el-input v-model.trim="userInfo.account" maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            type="password"
            v-model.trim="userInfo.password"
            @keyup.enter.native="submitForm('userInfo')"
          ></el-input>
        </el-form-item>
        <div class="save-psd">
          <el-checkbox v-model="checked">记住密码</el-checkbox>
        </div>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm('userInfo')"
            class="submit-btn"
            :loading="loading"
          >登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 登录
import { menu, storge, check } from '@/utils'
import { login } from '@/api/login'
export default {
  name: 'login',
  data() {
    const checkPassword = (rule, value, callback) => {
      if (!value) {
        callback('请输入密码')
      } else {
        callback()
      }
    }
    const checkAccount = (rule, value, callback) => {
      if (!value) {
        callback('请输入账号')
      } else {
        callback()
      }
    }
    return {
      loading: false,
      checked: true,
      userInfo: {
        account: '',
        password: ''
      },
      rules: {
        account: [{ required: true, validator: checkAccount, trigger: 'blur' }],
        password: [
          { required: true, validator: checkPassword, trigger: 'blur' }
        ]
      }
    }
  },
  beforeMount() {
    this.getUserAccount()
  },
  methods: {
    // 设置用户账号
    setUserAccount() {
      const value = escape(window.btoa(JSON.stringify(this.userInfo)))
      storge.setLocalStorge('account', value)
    },
    // 获取用户账号
    getUserAccount() {
      const value = storge.getLocalStorge('account')
      if (value) {
        this.userInfo = JSON.parse(window.atob(unescape(value)))
      }
    },
    // 删除用户信息
    deleteUserAccount() {
      storge.clearLocalStorge('account')
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          const {
            options: { routes }
          } = this.$router
          this.loading = true
          login(this.userInfo)
            .then(res => {
              const { me, token_type, access_token } = res
              // 保存账号
              if (this.checked) {
                this.setUserAccount()
              } else {
                this.deleteUserAccount()
              }
              storge.setSessionStorage('token', `${token_type}${access_token}`)
              storge.setSessionStorage('user', me)
              // 从菜单页面返回，则直接跳过去，不进行菜单和路由处理
              if (routes[2].children.length) {
                this.$router.go(1)
              } else {
                menu.mergeMenu().then(path => {
                  this.loading = false
                  this.$router.push({
                    path
                  })
                })
              }
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100vw;
  height: 100vh;
  background: #f0f2f5;
  display: flex;
  justify-content: center;
  align-items: center;
  .content {
    width: 330px;
    border-radius: 5px;
    display: flex;
    flex-flow: wrap;
    h3 {
      margin-bottom: 30px;
      width: 100%;
      box-sizing: border-box;
    }
    .item {
      margin-bottom: 20px;
    }
    .save-psd {
      padding-left: 60px;
      box-sizing: border-box;
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      justify-content: flex-start;
    }
    .submit-btn {
      margin-top: 5px;
      width: 100%;
    }
  }
}
</style>